//
//  WFUserEditViewController.m
//  WisdomFarm
//
//  Created by 心情 on 2017/10/26.
//  Copyright © 2017年 sky. All rights reserved.
//

#import "WFUserEditViewController.h"
#import "WFUserEditCell.h"
#import "WFSeparatorTableViewCell.h"
#import "WFWFTextViewCell.h"
#import "WFTextFieldTableViewCell.h"
#import "WFButtonCell.h"
#import "WFButtonTableFooterView.h"
#import "UITableViewCell+WFHeight.h"

@interface WFUserEditViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) WFUserEditCell *avatarCell;
@property (nonatomic, strong) WFUserEditCell *phoneCell;
@property (nonatomic, strong) WFTextFieldTableViewCell *nicknameCell;
@property (nonatomic, strong) WFSeparatorTableViewCell *seperateCell;
@property (nonatomic, strong) WFUserEditCell *genderCell;
@property (nonatomic, strong) WFUserEditCell *locationCell;
@property (nonatomic, strong) WFSeparatorTableViewCell *secondSeperateCell;
@property (nonatomic, strong) WFUserEditCell *userTypeCell;
@property (nonatomic, strong) WFUserEditCell *signCell;
@property (nonatomic, strong) WFWFTextViewCell *signContentCell;
@property (nonatomic, strong) WFButtonTableFooterView *footerView;




@property (nonatomic, strong) NSArray<__kindof UITableViewCell *> *cells;


@end

@implementation WFUserEditViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    }
    return self;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"修改资料";
    
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _footerView = [[WFButtonTableFooterView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 40)];
    [self.view addSubview:self.footerView];
    [_footerView.saveButton addTarget:self action:@selector(onSave) forControlEvents:UIControlEventTouchUpInside];
    _footerView.canDelete = NO;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.insets(UIEdgeInsetsZero);
        make.bottom.equalTo(self.footerView.mas_top);
    }];
    [self.footerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.insets(UIEdgeInsetsZero);
        make.height.mas_equalTo(50);
    }];

    
    _avatarCell = [[WFUserEditCell alloc] initWithStyle:WFUserEditCellStyleImageWithArrow];
    _phoneCell = [[WFUserEditCell alloc] initWithStyle:WFUserEditCellStyleText];
    _nicknameCell = [[WFTextFieldTableViewCell alloc] init];
    _seperateCell = [[WFSeparatorTableViewCell alloc] init];
    _genderCell = [[WFUserEditCell alloc] initWithStyle:WFUserEditCellStyleTextWithArrow];
    _locationCell = [[WFUserEditCell alloc] initWithStyle:WFUserEditCellStyleTextWithArrow];
    _secondSeperateCell = [[WFSeparatorTableViewCell alloc] init];
    _userTypeCell = [[WFUserEditCell alloc] initWithStyle:WFUserEditCellStyleTextWithArrow];
    _signCell = [[WFUserEditCell alloc] initWithStyle:WFUserEditCellStyleText];
    _signContentCell = [[WFWFTextViewCell alloc] init];

    self.cells = @[ self.avatarCell, self.phoneCell, self.nicknameCell, self.seperateCell, self.genderCell, self.locationCell, self.secondSeperateCell, self.userTypeCell, self.signCell, self.signContentCell];
    
    WFUser *user = [WFStore currentUser];
    self.avatarCell.titleLabel.text = @"头像";
    if (user.avatarURL) {
        [self.avatarCell.imageView sd_setImageWithURL:user.avatarURL];
    }
    self.phoneCell.titleLabel.text = @"用户名";
    self.phoneCell.contentLabel.text = user.phone ?: @"";
    self.nicknameCell.titleLabel.text = @"昵称";
    self.nicknameCell.textField.text = user.nickname ?: @"";
    self.genderCell.titleLabel.text = @"性别";
    self.genderCell.contentLabel.text = user.genderStr ?: @"";
    self.locationCell.titleLabel.text = @"所在地";
    self.locationCell.contentLabel.text = user.area ?: @"";
    self.userTypeCell.titleLabel.text = @"用户类型";
    self.signCell.titleLabel.text = @"个人说明";
    self.signContentCell.textView.text = user.signature ?: @"";
    
    self.seperateCell.cellHeight = 10;
    self.secondSeperateCell.cellHeight = 10;
    self.avatarCell.cellHeight =
    self.phoneCell.cellHeight =
    self.nicknameCell.cellHeight =
    self.genderCell.cellHeight =
    self.locationCell.cellHeight =
    self.userTypeCell.cellHeight = 50;
    
    self.signCell.cellHeight = 38;
    self.signContentCell.cellHeight = 57;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewSafeAreaInsetsDidChange {
    [super viewSafeAreaInsetsDidChange];
    [self.footerView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.insets(UIEdgeInsetsZero);
        make.height.mas_equalTo(50 + self.view.safeAreaInsets.bottom);
    }];
}

- (void)onSave {
    //TODO:保存用户资料
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 9;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [self.cells[indexPath.row] cellHeight];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.cells[indexPath.row];
}


@end
